import React, { Component } from 'react'
import { View, Text, Navigator } from '@tarojs/components'
import { AtButton, AtGrid, AtNoticebar, AtTabBar } from 'taro-ui'
import './index.less'
import config from '../../config.json'
import Taro from '@tarojs/taro';

export default class Index extends Component<any, any> {

    constructor(props) {
        super(props);
        this.state = {
            authority: 3,
            count: 0,
            current: 0
        }
    }

    async componentWillMount() {
        let _this = this;
        //检查token
        let token = Taro.getStorageSync("token");
        if (!token) {
            Taro.navigateTo({
                url: "/pages/login/login"
            })
        } else {
            let res = await Taro.request({
                url: config.domainName + "/user/get",
                header: {
                    "token": Taro.getStorageSync("token")
                }
            });
            let countRes = await Taro.request({
                url: config.domainName + "/agreed/getcount",
                header: {
                    "token": Taro.getStorageSync("token")
                }
            });
            console.log(countRes.data)

            this.setState({
                authority: res.data.data.authority,
            })
        }
    }

    componentDidMount() { }

    componentWillUnmount() { }

    async componentDidShow() {
        let countRes = await Taro.request({
            url: config.domainName + "/agreed/getcount",
            header: {
                "token": Taro.getStorageSync("token")
            }
        });
        if (!countRes.data.code) {
            this.setState({
                count: countRes.data.data[0]["COUNT(*)"]
            });
        }
        Taro.setStorageSync("count", countRes.data.data[0]["COUNT(*)"])
    }

    componentDidHide() { }

    onClickItem(item: any) {
        // if(item.value === "统计") {
        //     Taro.showToast({title: "暂未完成，敬请期待", icon: "none"});
        //     return;
        // }
        if (item.to) {
            console.log(item.to)
            Taro.navigateTo({ url: item.to });
        }
    }

    handleClickTabBar(index) {
        if(index === 1 && index !== this.state.current) {
            Taro.navigateTo({url: "/pages/userCenter/userCenter"});
        }
    }

    render() {
        return (
            <View className='index'>
                <View className="module-title">西邮会议</View>
                {this.state.count ? <AtNoticebar>您有{this.state.count}条申请尚未处理</AtNoticebar> : ""}
                <AtGrid onClick={this.onClickItem.bind(this)} hasBorder={false} data={getRouter(this.state.authority, this.state.count)} />
                <AtTabBar
                    className="tabbar"
                    fixed
                    tabList={[
                        { title: '菜单', iconType: 'bullet-list', text: this.state.count?this.state.count:null },
                        { title: '个人空间', iconType: 'user' } 
                    ]}
                    onClick={this.handleClickTabBar.bind(this)}
                    current={this.state.current}
                />
            </View>

        )
    }
}

function getRouter(authority: number, count: number): any[] {
    if (authority === 1) {
        return [
            {
                image: config.domainName + "/预定会议室.png",
                value: "预定会议室",
                to: "/pages/roomList/roomList"
            },
            {
                image: config.domainName + "/会议室管理.png",
                value: "管理会议室",
                to: "/pages/meetingroom/meetingroom"
            },
            {
                image: config.domainName + "/审批.png",
                value: "审批",
                to: "/pages/approve/approve"
            },
            {
                image: config.domainName + "/统计.png",
                value: "统计",
                to: "/pages/statistics/statistics",
            },
            {
                image: config.domainName + "/部门设置.png",
                value: "管理系部",
                to: "/pages/department/department"
            },
            {
                image: config.domainName + "/我的申请.png",
                value: "我的申请",
                to: "/pages/myapprove/myapprove"
            },
            {
                image: config.domainName + "/用户管理.png",
                value: "成员管理",
                to: "/pages/usersadmin/usersadmin"
            }
        ]
    } else if (authority === 2) {
        return [
            {
                image: config.domainName + "/预定会议室.png",
                value: "预定会议室",
                to: "/pages/roomList/roomList"
            },
            {
                image: config.domainName + "/审批.png",
                value: "审批",
                to: "/pages/approve/approve",
                dot: count
            },
            {
                image: config.domainName + "/统计.png",
                value: "统计",
                to: "/pages/statistics/statistics",
            },
            {
                image: config.domainName + "/我的会议室.png",
                value: "我的会议室",
                to: "/pages/myRoom/myRoom"
            },
            {
                image: config.domainName + "/我的申请.png",
                value: "我的申请",
                to: "/pages/myapprove/myapprove"
            }
        ]
    } else {
        return [
            {
                image: config.domainName + "/预定会议室.png",
                value: "预定会议室",
                to: "/pages/roomList/roomList"
            },
            {
                image: config.domainName + "/我的申请.png",
                value: "我的申请",
                to: "/pages/myapprove/myapprove"
            }
        ]
    }
}